﻿using System;
using System.Linq;
using WebExam.DataEntities;
using WebExam.DataProvider;

namespace WebExam.SQLDataProvider
{
    public class STKRepository : BaseRepository<STK>, ISTKRepository
    {

        /// <summary>
        /// 获取试题信息
        /// </summary>
        /// <param name="start">开始位置</param>
        /// <param name="takeSize">获取条数</param>
        public IQueryable<STK> GetAll(int start, int takeSize)
        {
            using (ThisDb)
            {
                return ThisTable.OrderBy(p => p.TXNum).OrderBy(p => p.STTM).Skip(start).Take(takeSize);
            }
        }

        /// <summary>
        /// 获取指定题型下的试题信息
        /// </summary>
        /// <param name="txId">题型Id</param>
        /// <param name="start">开始位置</param>
        /// <param name="takeSize">获取条数</param>
        public IQueryable<STK> GetAll(int txId, int start, int takeSize)
        {
            using (ThisDb)
            {
                return ThisTable.Where(p => p.TXNum == txId).OrderBy(p => p.TXNum).OrderBy(p => p.STTM).Skip(start).Take(takeSize);
            }
        }

        /// <summary>
        /// 获取试题信息
        /// </summary>
        /// <param name="stId">试题Id</param>
        public STK Get(int stId)
        {
            using (ThisDb)
            {
                return ThisTable.SingleOrDefault(p => p.STNum == stId);
            }
        }

        /// <summary>
        /// 添加试题信息
        /// </summary>
        /// <param name="st">试题对象</param>
        public STK Add(STK st)
        {
            using (ThisDb)
            {
                try
                {
                    ThisTable.InsertOnSubmit(st);
                    ThisDb.SubmitChanges();
                    return st;
                }
                catch
                {
                    return st;
                }
            }
        }

        /// <summary>
        /// 更新试题信息
        /// </summary>
        /// <param name="stId">试题Id</param>
        /// <param name="update">包含更新操作的方法</param>
        public STK Update(int stId, Action<STK> update)
        {
            using (ThisDb)
            {
                var st = ThisTable.SingleOrDefault(p => p.STNum == stId);

                if (st == null)
                {
                    return null;
                }

                update(st);

                try
                {
                    ThisDb.SubmitChanges();
                    return st;
                }
                catch
                {
                    return null;
                }

            }
        }

        /// <summary>
        /// 删除试题信息
        /// </summary>
        /// <param name="stId">试题Id</param>
        public bool Delete(int stId)
        {
            using (ThisDb)
            {
                var st = ThisTable.SingleOrDefault(p => p.STNum == stId);

                if (st == null)
                {
                    return true;
                }

                try
                {
                    ThisTable.DeleteOnSubmit(st);
                    ThisDb.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 指定题型下是否存在同名试题
        /// </summary>
        /// <param name="txId">题型Id</param>
        /// <param name="stName">试题名称</param>
        public bool IsExists(int txId, string stName)
        {
            using (ThisDb)
            {
                return ThisTable.Count(p => p.TXNum == txId && string.Equals(p.STTM, stName, StringComparison.OrdinalIgnoreCase)) > 0;
            }
        }

        /// <summary>
        /// 统计试题条数
        /// </summary>
        public int Count()
        {
            using (ThisDb)
            {
                return ThisTable.Count();
            }
        }

        /// <summary>
        /// 统计指定题型下的试题条数
        /// </summary>
        /// <param name="txId">题型Id</param>
        public int Count(int txId)
        {
            using (ThisDb)
            {
                return ThisTable.Count(p => p.TXNum == txId);
            }
        }
    }
}